package com.training.schedule_management_system_admin.service;

import com.training.schedule_management_system_admin.entity.Scheduletemplate;
import com.training.schedule_management_system_admin.entity.ScheduletemplateExample;
import com.training.schedule_management_system_admin.entity.User;
import com.training.schedule_management_system_admin.mapper.ScheduletemplateMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class ScheduletemplateService {

    @Autowired
    ScheduletemplateMapper scheduletemplateMapper;

    /*
    当前用户下的日程模版
     */
    public List<Scheduletemplate> getAllWithUser() {
        //获取当前已登录的用户
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");

        ScheduletemplateExample example=new ScheduletemplateExample();
        ScheduletemplateExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(currentUser.getId());
        return scheduletemplateMapper.selectByExample(example);
    }

    /*
    创建日程模版
     */
    public int create(Scheduletemplate template) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User currentUser = (User)session.getAttribute("loginUser");
        template.setUserId(currentUser.getId());
        return  scheduletemplateMapper.insertSelective(template);
    }

    /*
    通过id查找日程模版
     */
    public Scheduletemplate getById(Integer id) {
        return scheduletemplateMapper.selectByPrimaryKey(id);
    }

    /*
    修改日程模版
     */
    public int update(Scheduletemplate template) {
        return scheduletemplateMapper.updateByPrimaryKeySelective(template);
    }
}
